{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0f639efe",
   "metadata": {},
   "source": [
    "# 字典dict（）方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77518227",
   "metadata": {},
   "source": [
    "## clear（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "752d7f1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "food.clear()                                          #clean（）用于清空字典dict（）的key-value对\n",
    "print(food)                         "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7b92a86",
   "metadata": {},
   "source": [
    "## pop()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a2ff6c3a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'hamburger': 15, 'fruit': 30}\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "food.pop('bread')                                          \n",
    "print(food) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8a91992",
   "metadata": {},
   "source": [
    "## popitem（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7bb8bbcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('fruit', 30) <class 'tuple'>\n",
      "{'hamburger': 15}\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "print(food.popitem(),type(food.popitem()))          #popitem，随机删除一个键对值\n",
    "print(food)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "857e141f",
   "metadata": {},
   "source": [
    "## setdefault（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1fdcbd39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "None\n",
      "{'hamburger': 15, 'bread': 12, 'fruit': 30, 14: 15, 15: None}\n",
      "17\n",
      "{'hamburger': 15, 'bread': 12, 'fruit': 30, 14: 15, 15: None, 18: 17}\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "print(food.setdefault(14,15))\n",
    "print(food.setdefault(15))                        #有责查，返回value值，无则增加 a.setdefault(key,value)若value省略。则默认value值为None\n",
    "print(food)\n",
    "print(food.setdefault(18,17))\n",
    "print(food)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56704fcd",
   "metadata": {},
   "source": [
    "## update（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "018f7344",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'hamburger': 20, 'bread': 16, 'fruit': 35}\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "food.update({'hamburger': 20, 'bread': 16, 'fruit': 35})   # a.update({key,value}),更新key的value值                                         \n",
    "print(food)  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3abea64",
   "metadata": {},
   "source": [
    "## get（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1faa8995",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "print(food.get('bread'))          # a.get（输入key，取出value），若key值不存在，返回none                                        \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23d66002",
   "metadata": {},
   "source": [
    "## keys（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a98dfe7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "print(food.keys(),type(food.keys()))                  #keys，取出所有的key值，返回dict_keys"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a02effa",
   "metadata": {},
   "source": [
    "## values（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3b8f3b05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([15, 12, 30]) <class 'dict_values'>\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "print(food.values(),type(food.values()))              #values，取出所有的value值，返回dict_values"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3704a344",
   "metadata": {},
   "source": [
    "## items（）方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "261b3d10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('hamburger', 15), ('bread', 12), ('fruit', 30)]) <class 'dict_items'>\n"
     ]
    }
   ],
   "source": [
    "food = {'hamburger': 15, 'bread': 12, 'fruit': 30}\n",
    "print(food.items(),type(food.items()))               #items,取出全部的键值队，返回dice_items"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
